Fix phpstan/phpstan#14439: infinite loop#5426
Merged
staabm merged 1 commit intophpstan:2.1.xfrom Apr 8, 2026
Merged
Conversation
Contributor
|
Seems very related with phpstan/phpstan#4957 |
staabm
approved these changes
Apr 8, 2026
Contributor
There was a problem hiding this comment.
verified the test and fix. I think it looks good.
can also confirm that the fix works for phpstan/phpstan#4957
(I did not add another regresssion test for it, because they are very similar)
endless backtrace is
/Users/m.staab/dvl/phpstan-src/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/PhpDocNode.php:33
/Users/m.staab/dvl/phpstan-src/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/PhpDocNode.php:41
/Users/m.staab/dvl/phpstan-src/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/PhpDocNode.php:138
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/PhpDocNodeResolver.php:238
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/ResolvedPhpDocBlock.php:511
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:1993
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:1978
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:233
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:486
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:831
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:182
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/PhpDocNodeResolver.php:240
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/ResolvedPhpDocBlock.php:511
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:1993
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:1978
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:233
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:486
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:831
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:182
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/PhpDocNodeResolver.php:240
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/ResolvedPhpDocBlock.php:511
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:1993
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:1978
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:233
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:486
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:831
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:182
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/PhpDocNodeResolver.php:240
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/ResolvedPhpDocBlock.php:511
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:1993
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:1978
/Users/m.staab/dvl/phpstan-src/src/Reflection/ClassReflection.php:233
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:486
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:831
/Users/m.staab/dvl/phpstan-src/src/PhpDoc/TypeNodeResolver.php:182
...
- Use native reflection instead of getParentClass() when resolving `parent` type in TypeNodeResolver - getParentClass() resolves @extends PHPDoc tags, which can create infinite recursion when the tag itself uses `parent` - New integration test in tests/PHPStan/Analyser/AnalyserIntegrationTest.php
eb5ecd6 to
4bc0117
Compare
VincentLanglet
approved these changes
Apr 8, 2026
This was referenced Apr 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Using
@extends parent<T>in PHPDoc caused an infinite loop / memory exhaustion. Theparentkeyword in a generic@extendstag triggered recursive PHPDoc resolution.Changes
src/PhpDoc/TypeNodeResolver.phpto usegetNativeReflection()->getParentClass()instead ofgetParentClass()when resolving theparenttype identifiertests/PHPStan/Analyser/AnalyserIntegrationTest.phpwith test data intests/PHPStan/Analyser/data/bug-14439.phpRoot cause
When TypeNodeResolver resolved the
parentidentifier in PHPDoc, it calledClassReflection::getParentClass(). This method resolves@extendsPHPDoc tags to determine the generic parent type. When the@extendstag itself usedparent(e.g.,@extends parent<T>), it triggered the same resolution again, creating infinite recursion.The fix uses
getNativeReflection()->getParentClass()which gets the parent class directly from PHP's reflection without going through PHPDoc resolution, breaking the cycle.Test
Added
testBug14439inAnalyserIntegrationTestwith a test case containing a class hierarchy using@extends parent<T>that previously caused an infinite loop.Fixes phpstan/phpstan#14439
Closes phpstan/phpstan#4957